const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const jwtSecret = "jwtSecret";
const bcryptSalt = 10;

async function verifyToken(token) {
  await jwt.verify(token, jwtSecret, (err) => {
    if (err) {
      return false;
    }
  });
  return true;
}

function hashPassWordSync(pwd) {
  const salt = bcrypt.genSaltSync(bcryptSalt);
  const hash = bcrypt.hashSync(pwd, salt);
  return hash;
}

function comparePasswordSync(userPwd, dbPwd) {
  return bcrypt.compareSync(userPwd, dbPwd);
}

function signTokenByUsername(username) {
  const token = jwt.sign({ username }, jwtSecret, { expiresIn: "24h" });

  return token;
}

module.exports = {
  hashPassWordSync,
  comparePasswordSync,
  verifyToken,
  signTokenByUsername,
};
